//
//  ViewController.m
//  TMS
//
//  Created by Riddhi Joshi on 23/01/15.
//  Copyright (c) 2015 Riddhi Joshi. All rights reserved.
//

#import "ViewController.h"
#import "ListCourseViewController.h"
@interface ViewController ()

@property (assign) int traineeId;

@end

@implementation ViewController


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.Name.delegate=self;
    self.Password.delegate=self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    
    //for getting the view stacked on navigation controller
    UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
    ListCourseViewController *destViewController =(ListCourseViewController *)[navController topViewController];
    
   //ListCourseViewController *destViewController=[segue destinationViewController];
   
    NSLog(@"In ViewController trainee id is %d",self.traineeId);
    destViewController.TraineeId=self.traineeId;
    
    
}
- (IBAction)onClickLogin:(id)sender {
    
  
    NSString *name=self.Name.text;
    NSString *password=self.Password.text;
    
    //NSArray *resultArray=[name componentsSeparatedByString:[NSCharacterSet characterSetWithCharactersInString:@".@"]];
    
    
    if ([name isEqualToString:@""] || [password isEqualToString:@""] )
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Entry Error" message:@"Name or Password Cannot Be Nil" delegate:self cancelButtonTitle:@"Clear" otherButtonTitles:@"ok", nil];
        [alert show];

    }
    else
    {
    
        self.TraineeTable=[[TraineeTable alloc]init];
        self.traineeId=[self.TraineeTable Login:name andPassword:password];
    
        if(self.traineeId>0)
        {
        
            [self performSegueWithIdentifier:@"showTraineeHome" sender:self];
        }
        else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Entry Error" message:@"Name or Password Does Not Match" delegate:self cancelButtonTitle:@"Clear" otherButtonTitles:@"ok", nil];
        [alert show];
        }
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == alertView.cancelButtonIndex)
    {
        self.Name.text=nil;
        self.Password.text=nil;
    }
    else if(buttonIndex ==1)
    {
        // do nothing
    }
}
@end
